package com.heima.behavior.service.impl;
import java.util.Date;
import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.article.mess.UpdateArticleMess;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @作者 itcast
 * @创建日期 2021/4/22 10:18
 **/
@Service
@Transactional
@Slf4j
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        // 校验参数 (通过校验注解检查)
        // 查询相关行为实体
        // 获取当前登录用户
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry;
        if(user!=null){
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),null);
        }else {
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null,dto.getEquipmentId());
        }
        if(apBehaviorEntry == null){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST,"相关行为实体数据不存在");
        }
        // 查询对应点赞行为是否存在s
        ApLikesBehavior likesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId()));
        if(likesBehavior == null){
            // 添加行为实体数据
            likesBehavior = new ApLikesBehavior();
            likesBehavior.setEntryId(apBehaviorEntry.getId());
            likesBehavior.setArticleId(dto.getArticleId());
            likesBehavior.setType((short)0);
            likesBehavior.setOperation(dto.getOperation());
            likesBehavior.setCreatedTime(new Date());
            save(likesBehavior);
            // =======================新加代码==========================
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            mess.setArticleId(dto.getArticleId());
            kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, UUID.randomUUID().toString(), JSON.toJSONString(mess));
            log.info("点赞行为 发送kafka消息 ==>{}",JSON.toJSONString(mess));
            // =======================新加代码==========================


        }else {
            // 修改行为实体数据
            likesBehavior.setOperation(dto.getOperation());
            updateById(likesBehavior);
        }
        return ResponseResult.okResult();
    }
}
